package com.wt.ecommerce.stream.custom;

import com.alibaba.fastjson.JSON;
import com.wt.ecommerce.vo.CommonMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.support.MessageBuilder;

/**
 * <h1>使用自定义的通信信道 QinyiSource 实现消息的发送</h1>
 */
@Slf4j
@EnableBinding(CustomSource.class)
public class CustomSendService {

    private final CustomSource customSource;

    public CustomSendService(CustomSource customSource) {
        this.customSource = customSource;
    }

    /**
     * <h2>使用自定义的输出信道发送消息</h2>
     */
    public void sendMessage(CommonMessage message) {

        String _message = JSON.toJSONString(message);
        log.info("in CustomSendService send message: [{}]", _message);
        customSource.qinyiOutput().send(
                MessageBuilder.withPayload(_message).build()
        );
    }
}
